package com.chexiehui.jc.mvp.car;

import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.StringUtils;
import com.chexiehui.jc.Constants;
import com.chexiehui.jc.base.ApiCallback;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BasePage;
import com.chexiehui.jc.base.BasePresenter;
import com.chexiehui.jc.entity.CarInfo;
import com.chexiehui.jc.entity.DictInfo;

import java.util.List;

/**
 * @Description 分期购车
 * @Auth SongJun
 * @Date 2020/05/21 00:18
 **/
public class StagesBuyCarListPresenter extends BasePresenter<StagesBuyCarListCovenant.View, StagesBuyCarListCovenant.Stores>
        implements StagesBuyCarListCovenant.Presenter {

    public StagesBuyCarListPresenter(StagesBuyCarListCovenant.View view) {
        attachView(view);
    }


    @Override
    public void getList(int pageIndex) {
        addSubscription(appStores.getList(mvpView.getCityName(), mvpView.getDescStr(), mvpView.getBrandId(), /*mvpView.getSeriesId(),*/
                mvpView.getDownPaymentsMin(), mvpView.getDownPaymentsMax(), mvpView.getMonthlyLawMin(), mvpView.getMonthlyLawMax(),
                mvpView.getMinPrice(), mvpView.getMaxPrice(), mvpView.getGradeType(), mvpView.getCountryType(),
                pageIndex, 20),
                new ApiCallback<BaseModel<BasePage<CarInfo>>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<BasePage<CarInfo>> model) {
                        if (model.getResultCode() == 0) {
                            if (model.getData() != null && model.getData().getItems() != null && model.getData().getItems().size() > 0) {
                                mvpView.onGetListSuccess(model);
                            } else {
                                onFailure(Constants.PAGE_DATA_EMPTY, "暂无信息");
                            }
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.onGetListFailure(new BaseModel<>(code, msg));
                    }
                });
    }

    @Override
    public void addShopInquiry(String carid, String shopids, String phone, String name, String city) {
        if (StringUtils.isTrimEmpty(carid)) {
            mvpView.showToast("请选择车系");
            return;
        }
        if (StringUtils.isTrimEmpty(phone)) {
            mvpView.showToast("请输入手机号");
            return;
        }
        if (!RegexUtils.isMobileSimple(phone)) {
            mvpView.showToast("请输入正确手机号");
            return;
        }
        if (StringUtils.isTrimEmpty(name)) {
            mvpView.showToast("请输入姓名");
            return;
        }
        if (StringUtils.isTrimEmpty(city)) {
            mvpView.showToast("请选择城市");
            return;
        }
        if (StringUtils.isTrimEmpty(shopids)) {
            mvpView.showToast("请选择经销商");
            return;
        }
        mvpView.showLoading("正在提交");
        addSubscription(appStores.addShopInquiry("1", carid, shopids, phone, name, city),
                new ApiCallback<BaseModel<Object>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<Object> model) {
                        if (model.getResultCode() == 0) {
                            mvpView.hide();
                            mvpView.onAddShopInquirySuccess(model);
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.hide();
                        mvpView.onAddShopInquiryFailure(new BaseModel<>(code, msg));
                    }
                });
    }


    @Override
    public void getCraGradeList() {
        addSubscription(appStores.getCraGradeList(""),
                new ApiCallback<BaseModel<List<DictInfo>>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<List<DictInfo>> model) {
                        if (model.getResultCode() == 0 && model.getData() != null && model.getData().size() > 0) {
                            mvpView.onGetCraGradeListSuccess(model);
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.onGetCraGradeListFailure(new BaseModel<>(code, msg));
                    }
                });
    }

    @Override
    public void getCountryTypeList() {
        addSubscription(appStores.getCountryTypeList(""),
                new ApiCallback<BaseModel<List<DictInfo>>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<List<DictInfo>> model) {
                        if (model.getResultCode() == 0 && model.getData() != null && model.getData().size() > 0) {
                            mvpView.onGetCountryTypeListSuccess(model);
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.onGetCountryTypeListFailure(new BaseModel<>(code, msg));
                    }
                });
    }
}